/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package word_help;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author luotao
 */
public class InitWord {

    private static final String PATH = "/Users/luotao/project/JProj_NB/word_help/src/words";
    private List<Period> perds = new ArrayList<Period>();
    private Unit currentUnit;

    public void init() throws Exception {
        
        File parent = new File(PATH);
        File[] child = parent.listFiles();
        int index = 0;
        for (File file : child) {
            index++;
            processWordFile(file, index);
        }

    }

    public void processWordFile(File file, int file_index) throws Exception {
        Period period = new Period();
        perds.add(period);
        period.setId(file_index);
        FileReader reader = new FileReader(file);
        BufferedReader in = new BufferedReader(reader);

        String s;
        while ((s = in.readLine()) != null) {
            if ("".equals(s.trim())) {
                continue;
            }
            if (isUnit(s)) {
                Unit unit = new Unit();
                unit.setName(s.toUpperCase());
                period.addUnit(unit);
                currentUnit = unit;
            } else {
                Word word = new Word();
                word.exactorStr(s);
                if (word.getEnName() == null || word.getEnName().trim().equals("")) {
                    continue;
                }
                currentUnit.addWord(word);

            }
        }
        in.close();
    }

    public boolean isUnit(String src) {
        Pattern p = Pattern.compile("Unit\\s*\\d", Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(src);
        boolean result = matcher.find();
        return result;
    }

    public List<Period> getPerds() {
        return perds;
    }

    public static void main(String[] args) throws Exception {

        InitWord initWord = new InitWord();
        initWord.init();
//        initWord.isUnit("unit 9");
    }
}
